var vm = new Vue({
  el: '#managementPage',
  data: function () {
    var checkPhone = function (rule, value, callback) {
      var phoneReg = /^1[0-9]{10}$/
      if ((!value) || phoneReg.test(value)) {
        callback()
      } else {
        callback(new Error('电话号码格式不正确'))
      }
    };
    var checkPassword = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('登录密码不能为空'));
      } else if (value.length < 3) {
        callback(new Error('登录密码不能少于3个字符！'));
      } else if (value === vm.personBaseInfo.loginName) {
        callback(new Error('登录名和密码不能相同！'));
      } else {
        callback();
      }
    };
    var checkNum = function (rule, value, callback)  {
      if(value){
        if (!Number(value)||!(value%1 === 0)) {
          callback(new Error('请输入正确的次数'));
        } else {
          if (Number(value) < 0) {
            callback(new Error('劳动合同签订次数必须大于0！'));
          } else {
            callback();
          }
        }
      }else {
        callback();
      }

    };
    return {
      treeActive: '01',
      loading: true,  // body loading
      fullHeight: document.documentElement.clientHeight, // 全屏高度
      treeData: [], // 左侧组织架构树结构数据
      rightItemData: [], // 右侧下级部门列表
      tableData: [], // 右侧部门人员数据
      parentId: '', // 组织架构pId
      id: '',  // 组织架构id
      filterText: '', // 左侧树 搜索框value
      treeLeftHeight: document.documentElement.clientHeight - 100, // 左侧树 的高度
      defaultProps: {
        label: 'name',
        isLeaf: 'leaf'
      }, // tree结构 懒加载defaultProps
      defaultPropsAdmin: {
        label: 'name',
        isLeaf: 'leaf',
        disabled: 'orgId'
      }, // tree结构 懒加载defaultProps
      pageSize: 10, // 右侧部门人员分页信息
      page: 1, // 右侧部门人员分页信息
      total: '', // 右侧部门人员分页信息
      showPagination: false, // 右侧部门人员是否显示分页
      treeShowNode: [], // 左侧组织架构树结构展开的节点
      orgName: '', // 组织架构name
      navItemObj: [], // 右侧bread导航
      rightClickFlag: false,
      leftSelId: '', // 左侧树右键选中id
      leftSelPid: '', // 左侧树右键选中pid
      leftSelNode: '', // 左侧树右键选中节点
      leftSelData: '', // 左侧树右键选中pid
      multipleSelection: '', // 部门人员选中状态集合
      selectedIDs: [], // 部门人员选中状态id数组
      selectedIDsChange: [],
      breadNavPid: [], // 面包屑导航id集合
      orgType: '',  // 组织角色type
      leftOrgTree: true,  // 组织架构树是否展示  默认为true
      leftOrgTree2:true,
      searchOrgTree: [], // 组织架构树搜索组织列表
      searchManTree: [], // 组织架构树搜索人员列表
      infoSideActive: 'baseInfo', // 人员信息右侧栏tab默认展示基本信息
      orgInfoSideActive: 'orgBaseInfo', // 部门组织信息tab 默认展示部门信息
      editOrgInfoSide: true,
      orgInfo: false, // 部门组织信息右侧栏是否展示  默认不展示
      // loginPassword: '',
      personBaseInfo: {
        isPwdEncrypted: '1',
        isActive: '1',
        isRejectSms: '0',
        enableStrongRemind: '0',
        userSex: '0',
        // loginPwd: ''
      }, // 人员基础信息
      showRightInfo: false,  // 右侧滑是否展示  默认为FALSE
      showRightOrgInfo: false, // 右侧滑是否展示  默认为FALSE
      showOverlay: false,  // 右侧滑遮罩层是否展示  默认为FALSE
      dialogSaveOrg: true, // 新增编辑组织弹窗是否展示
      rootNodeOrgCode: '', // 根目录orgCode
      rootNodeOrgId: '', // 根目录orgId
      saveOrgdata: {}, // 新增编辑组织保存数据
      apartmentList: [], // 人员所属部门列表
      regionList: [], // 人员所属部门列表
      dialogSelApartment: true, // 展示选择部门弹窗
      dialogSelRegion:true, // 展示选择行政区域弹窗
      dialogSetAdmin: true,  // 是否展示设置管理员窗口
      selApartmentData: [], // 选择部门tree结构
      selRegionData: [], // 选择行政区域tree结构
      setAdminTreeData: [], // 设置管理员tree结构
      apartCheckedList: [], // 已选部门id
      selSetAdminCheckedList: [], // 已选管理员id
      regionCheckedList: [], // 已选行政区域id
      apartCheckedExpanded: [],
      setAdminExpanded: [],
      regionCheckedExpanded: [],
      selApartmentList: [], // 选择部门列表
      orgPosRootNodeId: '', // 部门选择树根元素ID
      regionRootNodeId: '', // 行政区域选择树根元素ID
      setAdminRootNodeId: '',  // 设置管理员树根元素id
      userId: '',
      userOrgInfoFlag: true,
      filterText1: '', // 修改所属部门弹窗搜索部门
      filterText2: '', // 设置管理员查询人员
      filterText3: '', // 设置行政区域
      userName: '', // 编辑用户userName
      userNameFlag: true,
      userNameFlag1: false,
      newUserInfo: false,  // 是否为新增用户
      isDisabled: false, // 判断按钮是否可点击
      infoRules: {  // 添加成员校验
        userName: [
          {required: true, message: '姓名不能为空', trigger: 'blur'},
        ],
        userMobile: [{validator: checkPhone, trigger: 'blur'}],
        loginName: [
          {required: true, message: '登录名不能为空', trigger: 'blur'},
        ],
        // loginPwd: [
        //   {required: true, validator: checkPassword, trigger: 'blur'}
        // ],
        laborContractCount: [
          {validator: checkNum, trigger: 'blur'}
        ]
      },
      isFirst: true,  // 是否第一次加载组织架构树
      selOpuAcRoleList: [],  // 角色授权应用option
      selOpuAcRole: '',  // 已选角色授权应用
      firstGetSelOpuAcRole: true, // 是否第一次加载角色授权应用option
      OpuAcRoleItem: [],  // 角色授权应用下对应角色列表
      selRoleItemIDs: '',  // 所有选中角色
      userSearchOrgTree: [], // 选择组织岗位搜索 组织列表
      userSearchPosTree: [], // 选择组织岗位搜索 岗位列表
      orgAllInfo: {}, // 组织详细信息
      highlightCurrentMan: true, // 人员table 是否高亮展示当前行
      editOrgInfoFlag: false,
      showRightClick: false,
      orgInfoLabel: '部门信息',
      listOrgDeptData: [], // 下级部门列表
      allListOrgDeptData: [], // 下级部门列表
      listOrgUnitData: [], // 下级单位列表
      allListOrgUnitData: [], // 下级单位列表
      listOrgGroupData: [], // 下级工作组列表
      allListOrgGroupData: [], // 下级工作组列表
      showOrgDeptMore: false,  // 下级部门列表是否可查看更多
      hideOrgDeptMore: false,  // 下级部门列表是否可查看更多
      showOrgUnitMore: false,  // 下级单位是否可查看更多
      hideOrgUnitMore: false,  // 下级单位是否可查看更多
      showOrgGroupMore: false,  // 下级工作组是否可查看更多
      hideOrgGroupMore: false,  // 下级工作组是否可查看更多
      expandAllAdminNode: false,
      setAdminList: [],
      setAdminSearchFlag: true,
      searchAdminTree: [], // 设置组织管理员查询人员
      baseSeachAdminList: [],
      selRegionList:[],
      userRegionInfoFlag:true,
      orgId:'',
      userSex: '',
      userCount: '',
      // 新版本行政区域编码选择
      dialogSetRegion: false,
      setRegionTreeData: [],
      setRegionExpanded: [],
      defaultRegionProps:{
          children: 'children',
          label: 'label'
      },
      fileList: [],
      ctx: ctx,
      orgIdP: '',
      deptActiveIndex: '1',
      importMessageList: [],
      importFileResultDialog: false,
      isFoShan: false,

      currentRow:{},
      unitCategoryTypeList:[],
      deptDataLoadig:false,
      dialogAddDept:false,
      multipleSelection2: [],
      ogArry:[],
      dialogUpdateDept:false,
      selectVal:'',
      upDateLoading:false,
      delLoading:false,
      updateDeptOne:false,
      unitCategoryForm:{
        selectVal:'',
      },
      unitCategoryFormRules:{
        selectVal: [
          { required: true, message: '请选择单位类型', trigger: 'change' }
        ],
      },
      currentPage:1,
      pageSizes:[10,20,30,40,50],
      PageSize:10,
    }
  },
  mounted: function () {
    this.dialogSelApartment = false;
    this.dialogSetAdmin = false;
    this.dialogSelRegion = false;
    var that = this;
    window.addEventListener("resize", function () {
      return (function () {
        window.fullHeight = document.documentElement.clientHeight
        that.fullHeight = window.fullHeight;
        that.treeLeftHeight = that.fullHeight - 100;
      })();
    });
    // 加载行政区域
    that.loadRegion();
    that.initAllRootOrgId();
    that.getIsFoShan();
    that.getAeaUnitCategoryType();
  },
  methods: {
    treeExport: function(){
      var _that = this;
      var pId = _that.leftSelNodeData.orgId;
      window.location.href = ctx+'opu/om/org/export/orgAndUserInfo?parentOrgId='+pId;
    },
    // 导入组织
    importOrgAndUser: function (file) {
      var _that = this;
      if (_that.isFoShan) {
        var url = "opu/om/org/fs/import/orgAndUserInfo";
      } else {
        var url = "opu/om/org/import/orgAndUserInfo";
      }
      var orgId = _that.leftSelNodeData.orgId;
      if (!_that.isSpecifiedFileType(file.file.name)) {
        _that.$message({
          message: '不允许上传的文件类型',
          type: 'error'
        });
        _that.loadingFileWin = false;

        return false;
      }
      confirmMsg('', '确定导入所选文件吗？提示：此操作将会更新或新建组织、用户、角色用户数据', function () {
        _that.debounce(_that.uploadFileCom, file , url , orgId , true);
      });
    },
    importOrg: function (file) {
      var _that = this;
      if (_that.isFoShan) {
        //todo 判断条件是否需要修改？
        var url = "opu/om/org/fs/import/groupInfo";
      } else {
        var url = "civicism/unit/import/unitInfo";
      }
      //获取当前组织节点id
      //1.先从选中节点获取
      //2.获取不到，就是获取根节点
      var orgId = _that.orgIdP;
      if (!orgId) {
        orgId = _that.rootNodeOrgId;
      }
      if (!_that.isSpecifiedFileType(file.file.name)) {
        _that.$message({
          message: '不允许上传的文件类型',
          type: 'error'
        });
        _that.loadingFileWin = false;

        return false;
      }
      confirmMsg('', '确定导入所选文件吗？提示：此操作将会更新或新建组织数据', function () {
        _that.debounce(_that.uploadFileCom, file , url , orgId , true);
      });
    },
    importUser: function (file) {
      var _that = this;
      if (_that.isFoShan) {
        //todo 判断条件是否需要修改？
        var url = "opu/om/users/fs/import/userInfo";
      }else {
        var url = "civicism/linkman/import/userInfo";
      }
      //获取当前组织节点id
      //1.先从选中节点获取
      //2.获取不到，就是获取根节点
      var orgId = _that.orgIdP;
      if (!orgId) {
        orgId = _that.rootNodeOrgId;
      }
      if (!_that.isSpecifiedFileType(file.file.name)) {
        _that.$message({
          message: '不允许上传的文件类型',
          type: 'error'
        });
        _that.loadingFileWin = false;
        return false;
      }

      confirmMsg('', '确定导入所选文件吗？提示：此操作将会更新或新建用户数据', function () {
        _that.debounce(_that.uploadFileCom, file , url , orgId);
      });
    },
    importRoleUser: function (file) {
      var _that = this;
      var url= "opu/om/users/import/assignRoleToUser";
      //获取当前组织节点id
      //1.先从选中节点获取
      //2.获取不到，就是获取根节点
      var orgId = _that.orgIdP;
      if (!orgId) {
        orgId = _that.rootNodeOrgId;
      }
      if (!_that.isSpecifiedFileType(file.file.name)) {
        _that.$message({
          message: '不允许上传的文件类型',
          type: 'error'
        });
        _that.loadingFileWin = false;

        return false;
      }
      confirmMsg('', '确定导入所选文件吗？提示：此操作将会更新或新建角色用户数据', function () {
        _that.debounce(_that.uploadFileCom, file , url , orgId);
      });

    },
    debounce: function (func, file, url, orgId , needRefreshTree) {
      window.clearTimeout(func.tId);
      func.temArr = func.temArr || [];
      func.temArr.push(file);
      func.tId = window.setTimeout(function () {
        func(func.temArr , url , orgId , needRefreshTree);
        func.temArr = [];
      }, 1000);
    },
    // 上传
    uploadFileCom: function (file , url , orgId, needRefreshTree) {
      var _that = this;
      this.fileWinData = new FormData();
      file.forEach(function (u) {
        Vue.set(u.file, 'fileName', u.file.name);
        _that.fileWinData.append('file', u.file);
        _that.fileWinData.append('rootNodeOrgCode', _that.rootNodeOrgCode);
        _that.fileWinData.append('parentOrgId', orgId);
      })
      _that.loading = true;
      axios.post(ctx + url, _that.fileWinData).then(function (res) {
        if (res.data.success) {
          _that.loading = false;
          if(res.data.content) {
            _that.importFileResultDialog = true;
            _that.importMessageList = res.data.content.importMessageList;
            if(res.data.content.failMessage){
              res.data.content.failMessage.forEach(function (failInfo) {
                _that.importMessageList.push(failInfo);
              });
            }
          }
        }else {
            _that.loading = false;
            if(res.data.message){
              _that.importFileResultDialog=true;
              var resultStr=res.data.message.substring(0,res.data.message.length-1);
              _that.importMessageList=resultStr.split(";");
            }
        }
        if(res.data.content && res.data.content.successList && res.data.content.successList.length>0){
          _that.getListData(orgId, true);
          _that.showOverlay = false;
          _that.showRightOrgInfo = false;
          _that.resetForm('userOrgForm');
          if(needRefreshTree){
            res.data.content.successList.forEach(function (org) {
              if(org){
                var removeNode=_that.$refs.treeLeft.getNode(org.id);
                if(!removeNode){
                  _that.$refs.treeLeft.append(org, org.parentOrgId);
                }
              }
            });
          }
        }
      }).catch(function (error) {
        _that.loading = false;
        if (error.response) {
          _that.$message({
            message: '文件上传失败(' + error.response.data.status + ')，' + error.response.data.error,
            type: 'error'
          });
        } else if (error.request) {
          _that.$message({
            message: '文件上传失败，服务器端无响应',
            type: 'error'
          });
        } else {
          _that.$message({
            message: '文件上传失败，请求封装失败',
            type: 'error'
          });
        }

      });
    },
    // 判断文件类型是否存在规定的类型里
    isSpecifiedFileType: function (fileName) {
      var fileTypes = [".xlsx", ".xls"];
      var getFileType = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
      if (fileTypes.indexOf(getFileType) > -1) {
        return true;
      } else {
        return false;
      }
    },
    getListData: function (id, flag) {
    },
    // 更新左边的树
    refreshTree:function(){
      var _that = this;
      var treeActive = this.treeActive
      this.treeActive = '';
      setTimeout(function (){
        _that.treeActive = treeActive;
        _that.getOrgDeptData();
      },200)
    },
    // 获取水电气分类接口
    getAeaUnitCategoryType:function(){
      var _that = this;
      _that.loading = true;
      request('opus-admin', {url: ctx+'civicism/unit/getAeaUnitCategoryType/list', type: 'get'}, function (res) {
        _that.loading = false;
        if(res.success){
          _that.unitCategoryTypeList = res.content || [];
          _that.treeActive = _that.unitCategoryTypeList[0].itemCode;
        }else {
          _that.$message({
            message: msg.message ? msg.message : '请求失败！',
            type: 'error'
          });
        }
      }, function () {
        _that.loading = false;
      });
    },
    getOrgDeptData:function(){
      var _that = this;
      _that.deptDataLoadig = true;
      request('opus-admin', {
        url: ctx+'civicism/unit/orgAsyncZtree',
        type: 'get',
        data: {id: _that.id,categoryType:_that.treeActive}
      }, function (res) {
        if(res.success){
          _that.listOrgDeptData = res.content;
        }
        _that.deptDataLoadig = false;
      }, function () {
        _that.deptDataLoadig = false;
      })
    },
    // 批量新增单位。并关联单位类型
    saveUnitRelation:function(orgIds){
      var _that = this;
      var params ={
          "orgIds":orgIds,
          "categoryType":this.treeActive,
      }
      _that.isDisabled = true;
      request('opus-admin', {
        url: ctx+'civicism/unit/saveUnitRelation',
        type: 'post',
        ContentType: "application/json;charset=utf-8",
        data:JSON.stringify(params),
      }, function (res) {
        _that.isDisabled = false;
        if(res.success){
            _that.$message.success(res.message || '保存成功')
            _that.dialogAddDept = false;
            _that.refreshTree();

        }else {
          _that.$message({
            message: res.message ? res.message : '请求失败！',
            type: 'error'
          });
        }
      }, function () {
        _that.isDisabled = false;
      });
    },
    handleSelectionChange:function (val){
      this.multipleSelection2 = JSON.parse(JSON.stringify(val));
    },
    updateDept:function(){
      if(this.multipleSelection2.length > 0){
          this.ogArry = JSON.parse(JSON.stringify(this.multipleSelection2));
          this.dialogUpdateDept = true;
          debugger
          this.resetForm('unitCategoryForm');
      }else{
        this.$message({
          message: '请选择需要修改的单位！',
          type: 'warning'
        });
      }
    },
    sureUpDateDept:function(formName){
      var _that = this;
      this.$refs[formName].validate(function(valid){
        if (valid) {
          var orgIdsArry = [];
          debugger
          if(_that.updateDeptOne){
            orgIdsArry.push(_that.currentRow.orgId);
          }else{
            console.log(_that.multipleSelection2);
            console.log(_that.ogArry);
            orgIdsArry = _that.ogArry.map(function(item,index){
              return item.orgId;
            })
          }
          var params = {
            orgIds:orgIdsArry,
            categoryType:_that.unitCategoryForm.selectVal
          }
          _that.updateDeptOne = false;
          _that.updateUnitRelation(params);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    updateDeptOneFn:function(row){
      this.dialogUpdateDept = true;
      this.currentRow = row;
      this.updateDeptOne = true;
      this.resetForm('unitCategoryForm');
    },
    treeUpdateDeptFn:function (){
      this.dialogUpdateDept = true;
      this.currentRow = this.leftSelNodeData;
      this.updateDeptOne = true;
      this.resetForm('unitCategoryForm');
    },
    treedelDeptFn:function (){
      var _that = this;
      confirmMsg('确认信息', '是否确定删除？', function(){
        var orgIdsArry = [_that.leftSelNodeData.orgId]
        var params = {
          orgIds:orgIdsArry
        }
        _that.cancelUnitRelation(params);
      },function(){
        return false;
      },'确认删除','我再想想', 'warning', true)
    },
    // 更新关联单位类型
    updateUnitRelation:function(params){
      var _that = this;
      this.upDateLoading = true;
      request('opus-admin', {
        url: ctx+'civicism/unit/updateUnitRelation',
        type: 'post',
        ContentType: "application/json;charset=utf-8",
        data:JSON.stringify(params),
      }, function (res) {
        _that.upDateLoading = false;
        if(res.success){
          _that.$message({
            message: res.message ? res.message : '操作失败！',
            type: 'success'
          });
          _that.dialogUpdateDept = false;
          _that.refreshTree();
        }else {
          _that.$message({
            message: res.message ? res.message : '请求失败！',
            type: 'error'
          });
        }
      }, function () {
        _that.upDateLoading = false;
      });
    },
    delDept:function(){
      var _that = this;
      if(this.multipleSelection2.length > 0){
        confirmMsg('确认信息', '是否确定删除？', function(){
          var orgIdsArry = _that.multipleSelection2.map(function(item,index){
            return item.orgId;
          })
          var params = {
            orgIds:orgIdsArry
          }
          _that.cancelUnitRelation(params);
        },function(){
          return false;
        },'确认删除','我再想想', 'warning', true)
      }else{
        this.$message({
          message: '请选择需要移除掉的单位！',
          type: 'warning'
        });
      }
    },
    delDeptOne:function(item){
      var _that = this;
      confirmMsg('确认信息', '是否确定删除？', function(){
        var orgIdsArry = [item.orgId]
        var params = {
          orgIds:orgIdsArry
        }
        _that.cancelUnitRelation(params);
      },function(){
        return false;
      },'确认删除','我再想想', 'warning', true)
    },
    // 取消关联单位类型
    cancelUnitRelation:function(params){
      var _that = this;
      this.delLoading = true;
      request('opus-admin', {
        url: ctx+'civicism/unit/cancelUnitRelation',
        type: 'post',
        ContentType: "application/json;charset=utf-8",
        data:JSON.stringify(params),
      }, function (res) {
        _that.delLoading = false;
        if(res.success){
          _that.$message({
            message: res.message ? res.message : '操作失败！',
            type: 'success'
          });
          _that.refreshTree();
        }else {
          _that.$message({
            message: res.message ? res.message : '请求失败！',
            type: 'error'
          });
        }
      }, function () {
        _that.delLoading = false;
      });
    },
    loadNode1: function (node, resolve) { // 加载树数据
      var _that = this;
      if (node.level == 0) {
        request('opus-admin', {url: ctx+'civicism/unit/orgAsyncZtree',data:{categoryType:_that.treeActive}, type: 'get'}, function (data) {
          _that.treeData = [];
          data.content.forEach(function (e) {
            _that.treeData.push(e)
          })
          if(_that.treeData.length > 0){
            _that.treeShowNode[0] = _that.treeData[0].id;
            if (_that.isFirst) {
              _that.orgName = _that.treeData[0].orgName;
              _that.id = _that.treeData[0].id;
              _that.orgType = _that.treeData[0].type;
              _that.rootNodeOrgCode = _that.treeData[0].orgCode;
              _that.rootNodeOrgId = _that.treeData[0].id;
              _that.userCount = _that.treeData[0].userCount;
              _that.navItemObj = [
                {
                  pId: _that.treeData[0].pId,
                  name: _that.treeData[0].orgName,
                  id: _that.treeData[0].orgId,
                  userCount: _that.treeData[0].userCount
                }
              ];
            }
            if(_that.navItemObj.length>0){
              _that.orgIdP = _that.navItemObj[_that.navItemObj.length-1].id;
            }
            //_that.getListData(_that.treeData[0].id)
            _that.getOrgDeptData();
            _that.isFirst = false
          }
          resolve(_that.treeData);
          _that.loading = false;
        }, function () {
          resolve([]);
          _that.loading = false;
        });
      } else {
        request('opus-admin', {
          url: ctx+'civicism/unit/orgAsyncZtree',
          type: 'get',
          data: {id: node.data.id,categoryType:_that.treeActive}
        }, function (data) {
          var myList = [];
          if (data.content[0]) {
            _that.parentId = data.content[0].pId;
          }
          data.content.forEach(function (e) {
            myList.push(e)
          })
          _that.treeData = myList;
          resolve(_that.treeData);
          _that.loading = false;
        }, function () {
          resolve([]);
          _that.loading = false;
        })
      }
    },
    clickTreeNode: function (data) { // 树节点点击事件
      var _that = this;
      var ShowNode = data.orgSeq.split(".");
      var ShowNodeLen = ShowNode.length;
      _that.treeShowNode = ShowNode.slice(1, ShowNodeLen - 1);
      var navItem = data.orgNameSeq.split(",");
      var itemObj = [];
      _that.breadNavPid = ShowNode.slice(0, ShowNodeLen - 2);
      _that.treeShowNode.forEach(function (e, index) {
        itemObj[index] = {
          id: e
        };
      })
      _that.breadNavPid.forEach(function (e, index) {
        itemObj[index].pId = e
      })
      navItem.forEach(function (e, index) {
        itemObj[index].name = e;
      })

      _that.navItemObj = itemObj;
      if( _that.navItemObj.length>0){
        _that.orgIdP = _that.navItemObj[_that.navItemObj.length-1].id;
      }
      _that.pageSize = 10;
      _that.page = 1;
      _that.id = data.id;
      _that.orgName = data.orgName;
      _that.userCount = data.userCount;
      _that.name = data.name;
      _that.parentId = data.pId;
      //_that.getListData(data.orgId);
      _that.getOrgDeptData();
      setTimeout(function () {
        _that.$refs['treeLeft'][0].setCurrentKey(data.orgId);
      }, 500)
    },
    clickListNode: function (data) {
      this.showRightOrgInfo = false;
      this.resetForm('userOrgForm');
      this.showOverlay = false;
      this.clickTreeNode(data.row);
    },
    clickApartItem: function (data, type, id) {
      var _that = this;
      _that.resetForm('userOrgForm')
      _that.orgInfo = true;
      _that.userNameFlag = false;
      _that.userNameFlag1 = false;
      _that.parentOrgId = data.parentOrgId;
      if (type == 'u') {
        _that.orgInfoLabel = '添加下级单位'
        _that.editOrgInfoSide = false;
      } else if (type == 'd') {
        _that.orgInfoLabel = '添加下级部门'
        _that.editOrgInfoSide = false;
      } else if (type == 'g') {
        _that.orgInfoLabel = '添加工作组'
        _that.editOrgInfoSide = false;
      } else {
        _that.orgInfoLabel = '部门信息';
        _that.editOrgInfoSide = true;
      }
      if (data) {
        _that.editOrgInfoFlag = true;
        var orgId = data.orgId;
        _that.id = data.orgId;
        request('opus-admin', {url: ctx+'opu/om/org/' + orgId + '/org', type: 'get'}, function (res) {
          _that.orgAllInfo = res.content;
          _that.showOverlay = true;
          _that.showRightOrgInfo = true;
          _that.id = res.content.orgId;
          // _that.getRegionList([res.content]);
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message : '请求失败！',
            type: 'error'
          });
        });
      } else {
        _that.editOrgInfoFlag = false;
        var pId = '';
        if (id) {
          pId = id
        } else {
          pId = _that.leftSelNodeData.orgId;
          this.rightClickFlag = true;
        }
        request('opus-admin', {url: ctx+'opu/om/org/' + _that.rootNodeOrgCode, type: 'get'}, function (data) {
          if(data.success){
            _that.orgAllInfo = {};
            _that.orgAllInfo.orgCode = data.content;
            _that.showOverlay = true;
            _that.showRightOrgInfo = true;
            _that.orgAllInfo.parentOrgId = pId;
            _that.orgAllInfo.orgProperty = type;
          }else {
            _that.$message({
              message: data.message ? data.message : '生成编号失败！',
              type: 'error'
            });
          }
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message : '请求失败！',
            type: 'error'
          });
        });
      }
    },
    showOrgDeptMoreItem: function (data) { // 显示更多下级部门
      this.showOrgDeptMore = false;
      this.hideOrgDeptMore = true;
      this.listOrgDeptData = this.allListOrgDeptData;
    },
    hideOrgDeptMoreItem: function () { // 收起更多下级部门
      if (this.allListOrgDeptData.length > 4) { // 下级部门列表默认显示6条 超长隐藏
        this.showOrgDeptMore = true;
        this.hideOrgDeptMore = false;
        this.listOrgDeptData = this.allListOrgDeptData.slice(0, 5);
      } else {
        this.listOrgDeptData = this.allListOrgDeptData;
        this.showOrgDeptMore = false
        this.hideOrgDeptMore = false;
      }
    },
    showOrgUnitMoreItem: function (data) { // 显示更多下级部门
      this.showOrgUnitMore = false;
      this.hideOrgUnitMore = true;
      this.listOrgUnitData = this.allListOrgUnitData;
    },
    hideOrgUnitMoreItem: function () { // 收起更多下级部门
      if (this.allListOrgUnitData.length > 4) { // 下级部门列表默认显示6条 超长隐藏
        this.showOrgUnitMore = true;
        this.hideOrgUnitMore = false;
        this.listOrgUnitData = this.allListOrgUnitData.slice(0, 5);
      } else {
        this.listOrgUnitData = this.allListOrgUnitData;
        this.showOrgUnitMore = false
        this.hideOrgUnitMore = false;
      }
    },
    showOrgGroupMoreItem: function (data) { // 显示更多下级部门
      this.showOrgGroupMore = false;
      this.hideOrgGroupMore = true;
      this.listOrgGroupData = this.allListOrgGroupData;
    },
    hideOrgGroupMoreItem: function () { // 收起更多下级部门
      if (this.allListOrgGroupData.length > 4) { // 下级部门列表默认显示6条 超长隐藏
        this.showOrgGroupMore = true;
        this.hideOrgGroupMore = false;
        this.listOrgGroupData = this.allListOrgGroupData.slice(0, 5);
      } else {
        this.listOrgGroupData = this.allListOrgGroupData;
        this.showOrgGroupMore = false
        this.hideOrgGroupMore = false;
      }
    },
    returnItem: function () { // 返回上一级
      var _that = this;
      var len = _that.treeShowNode.length;
      var len2 = _that.navItemObj.length;
      _that.treeShowNode = _that.treeShowNode.splice(0, len - 1);
      var len1 = _that.treeShowNode.length;
      _that.$refs.treeLeft.setCurrentKey(_that.treeShowNode[len1 - 1]);
      if (len2 > 1) {
        _that.navItemObj = _that.navItemObj.slice(0, len2 - 1);
        if(_that.navItemObj.length>0){
          _that.orgIdP = _that.navItemObj[_that.navItemObj.length-1].id;
        }
        var len3 = _that.navItemObj.length;
        this.getListData(_that.treeShowNode[len1 - 1]);
        _that.orgName = _that.navItemObj[len3 - 1].name;
        _that.id = this.navItemObj[len3 - 1].id;
        request('opus-admin', {
          url: ctx+'/opu/om/org/userCount',
          type: 'get',
          data: {orgId : _that.id}
        }, function (data) {
          _that.userCount = data.content;
        });
      }
    },
    breadNavClick: function (id, pId, orgName, index) {  // 面包屑导航点击
      this.$refs.treeLeft.setCurrentKey(id);
      this.orgName = orgName;
      this.navItemObj = this.navItemObj.slice(0, index + 1);
      if(this.navItemObj.length>0){
        this.orgIdP = this.navItemObj[this.navItemObj.length-1].id;
      }
      this.id = id;
      this.parentId = id;
      this.getListData(id);
      this.pageSize = 10;
      this.page = 1;
      var _that = this;
      request('opus-admin', {
        url: ctx+'/opu/om/org/userCount',
        type: 'get',
        data: {orgId : id}
      }, function (data) {
        _that.userCount = data.content;
      });
    },
    treeNodeRightClick: function (event, data, Node) {  // 树节点右键事件
      this.showRightClick = true;
      $('.right-click-opt').css({'left': event.clientX, 'top': event.clientY});
      this.leftSelId = data.orgId;
      this.leftSelPid = data.pId;
      this.leftSelNode = Node;
      this.leftSelNodeData = data;
      this.rightClickFlag = true;
    },
    hideClickOpt: function () { // 关闭树节点右键弹窗
      this.showRightClick = false;
      this.rightClickFlag = false;
    },
    delTreeItem: function () {  // 删除组织
      var _that = this;
      var orgId = '';
      var removeNode = {};
      if (_that.rightClickFlag) {
        orgId = _that.leftSelId;
        removeNode = _that.leftSelNodeData;
      } else {
        orgId = _that.id;
        removeNode = _that.orgAllInfo;
      }
      confirmMsg('', '确定要删除选中的组织及自子级组织吗？', function () {
        _that.rightClickFlag = false;
        request('opus-admin', {url: ctx+'civicism/unit/' + orgId, type: 'delete'}, function (data) {
          _that.$message({
            message: '删除成功',
            type: 'success'
          });

          _that.getListData(_that.parentId);
          _that.$refs.treeLeft.remove(removeNode);
          _that.hideClickOpt();
          _that.showOverlay = false;
          _that.showRightOrgInfo = false;
          _that.resetForm('userOrgForm');
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message:'请求失败',
            type: 'error'
          });
        });
      })
    },
    handleSelManChange: function (val) { // 获取部门人员选中状态的id数组
      this.multipleSelection = val;
      var ids = new Array();
      this.multipleSelection.map(function (item) {
        ids.push(item.userId)
      })
      this.selectedIDsChange = ids;
      return this.selectedIDsChange;
    },
    delMember: function () { // 删除部门人员
      var _that = this;
      if(!_that.showRightInfo){
        _that.selectedIDs = _that.selectedIDsChange;
      }
      var delData = {
        _method: "DELETE",
        type: 'o',
        userIds: JSON.stringify(_that.selectedIDs)
      };
      if ((this.selectedIDs == null || this.selectedIDs.length == 0 || this.selectedIDs == 'undefined')&&(!_that.showRightInfo)) {
        _that.$message({
          message: '请选择要删除的人员',
          type: 'error'
        });
      } else {
        confirmMsg('', '确定要删除选中的组织及自子级组织人员吗？', function () {
          request('opus-admin', {url: ctx+'civicism/linkman/userIds', type: 'post', data: delData}, function (data) {
            if (data.success) {
              _that.$message({
                message: '删除成功',
                type: 'success'
              });
              _that.showOverlay = false;
              _that.showRightInfo = false;
            } else {
              _that.$message({
                message: data.message ? data.message:'删除失败',
                type: 'error'
              });
            }
          }, function (msg) {
            _that.$message({
              message: msg.message ? msg.message:'请求失败',
              type: 'error'
            });
          });
        })
      }
    },
    initUserPwd: function () { // 初始化人员密码
        var _that = this;
        var userId = _that.userId;
        request('opus-admin', {
            url: ctx + 'civicism/linkman/initUserPwd',
            type: 'get',
            data: {
              'userId': userId
            }
        }, function (result) {
            if (result.success) {
                _that.$message({
                    message: '初始化登录密码成功,密码为：' + result.content ,
                    type: 'success'
                });
            } else {
                _that.$message({
                    message: data.message ? data.message:'初始化登录密码出错!',
                    type: 'error'
                });
            }
        }, function (msg) {
            _that.$message({
                message: msg.message ? msg.message : '初始化登录密码出错!',
                type: 'error'
            });
        });
    },
    userBatchUpload: function () { // 批量上传人员
      var _that = this;
      if(!_that.showRightInfo){
        _that.selectedIDs = _that.selectedIDsChange;
      }
      var uploadData = {
        userIds: (_that.selectedIDs).toString() /*JSON.stringify(_that.selectedIDs)*/
      };
      if ((this.selectedIDs == null || this.selectedIDs.length == 0 || this.selectedIDs == 'undefined')&&(!_that.showRightInfo)) {
        _that.$message({
          message: '请选择要批量上传的人员！',
          type: 'error'
        });
      } else {
        confirmMsg('', '此操作将把用户信息上传至腾讯云,供及时通讯使用,您确定执行吗？', function () {
          request('opus-admin', {url: ctx+'opu/om/users/batchUploadUserToTencent',
            data: uploadData
          }, function (data) {
            if (data.success) {
              _that.$message({
                message: data.message,
                type: 'success'
              });
              _that.showOverlay = false;
              _that.showRightInfo = false;
            } else {
              _that.$message({
                message: data.message ? data.message:'上传失败',
                type: 'error'
              });
            }
          }, function (msg) {
            _that.$message({
              message: msg.message ? msg.message:'请求失败',
              type: 'error'
            });
          });
        })
      }

    },
    handleSizeChange: function (val) {
      this.PageSize=val
      this.currentPage=1
    },
    handleCurrentChange: function (val) {
      this.currentPage=val
    },
    getPersonBaseInfo: function (loginName) { // 获取用户信息
      var _that = this;
      request('opus-admin', {
        url: ctx+'opu/om/userInfos',
        type: 'get',
        data: {loginName: loginName}
      }, function (data) {
        _that.personBaseInfo = data.content;
        _that.userSex = data.content.userSex;
        _that.getApartmentList(data.content.userId)
        _that.userName = data.content.userName;
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '请求失败',
          type: 'error'
        });
      });
    },
    showManInfo: function (data) {
      this.infoSideActive = 'baseInfo';
      var _that = this;
      this.selectedIDs = [];
      this.userId = '';
      this.showRightInfo = true;
      if (data.userId) {
        this.selectedIDs.push(data.userId)
        this.getPersonBaseInfo(data.loginName)
        this.newUserInfo = false;
        this.userNameFlag = true;
        this.userNameFlag1 = false;
      } else {
        _that.id = data;
        this.personBaseInfo = {
          isPwdEncrypted: '1',
          isActive: '1',
          isRejectSms: '0',
          enableStrongRemind: '0',
          userSex: '0',
          // loginPwd: ''
        };
        this.apartmentList = [];
        this.apartCheckedList = [];
        this.userName = '';
        this.userNameFlag = false;
        this.userNameFlag1 = true;
        this.newUserInfo = true;
        for (var i = 1; i < _that.$refs.selApartmentTree.store._getAllNodes().length; i++) {
          _that.$refs.selApartmentTree.store._getAllNodes()[i].expanded = false;
        }
      }

      this.showOverlay = true;
      this.orgInfo = false;
    },
    newEditOrg: function () {
      var _that = this;
      request('opus-admin', {url: ctx+'opu/om/org/' + _that.rootNodeOrgCode, type: 'get'}, function (data) {
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '请求失败',
          type: 'error'
        });
      });
    },
    changeUserActive: function (dataSend,e) {
      e.cancelBubble = true;
      this.showRightInfo = false;
      this.showOverlay = false;
      var _that = this;
      request('opus-admin', {
        url: ctx+'/opu/om/users/enOrDisableIsActive',
        type: 'put',
        data: {userId: dataSend.userId}
      }, function (data) {
        if (data.success) {
          _that.$message({
            message: '修改成功',
            type: 'success'
          });
          var orgId = _that.navItemObj[_that.navItemObj.length-1].id;
        } else {
          _that.$message({
            message: data.message ? data.message : '修改失败，请稍后再试',
            type: 'error'
          });
        }
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '服务请求失败',
          type: 'error'
        });
      });
    },
    changeUserLock: function (dataSend,e) {
      e.cancelBubble = true;
      this.showRightInfo = false;
      this.showOverlay = false;
      var _that = this;
      request('opus-admin', {
        url: ctx+'opu/om/users/changeUserIsLock',
        type: 'put',
        data: {userId: dataSend.userId,isLock: dataSend.isLock}
      }, function (data) {
        if (data.success) {
          _that.$message({
            message: '修改成功',
            type: 'success'
          });
          var orgId = _that.navItemObj[_that.navItemObj.length-1].id;

        } else {
          _that.$message({
            message: data.message ? data.message : '修改失败，请稍后再试',
            type: 'error'
          });
        }
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '服务请求失败',
          type: 'error'
        });
      });
    },
    getApartmentList: function (userId) {
      var _that = this;
      _that.userId = userId;
      request('opus-admin', {
        url: ctx+'opu/om/users/getOpuOmOrgPosByUserId',
        type: 'get',
        data: {userId: userId}
      }, function (data) {
        _that.apartmentList = [];
        _that.apartCheckedList = [];
        _that.apartCheckedExpanded = [];
        for (var i = 0; i < _that.$refs.selApartmentTree.store._getAllNodes().length; i++) {
          _that.$refs.selApartmentTree.store._getAllNodes()[i].expanded = false;
        }
        data.map(function (item) {
          _that.apartCheckedList.push(item.id);
          _that.apartmentList.push({name: item.name, id: item.id, type: item.type, orgId: item.orgId})
          if (item.pId) {
            _that.apartCheckedExpanded.push(item.pId);
          }
        });
        _that.$refs.selApartmentTree.setCheckedKeys(_that.apartCheckedList, false);
        _that.selApartmentTreesetCheck(userId);
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '请求失败',
          type: 'error'
        });
      });
    },
    selApartmentTreesetCheck: function(uId){
      var _that = this;
      var selApartment = _that.$refs.selApartmentTree.getCheckedNodes();
      selApartment.map(function (item) {
        var obj = {
          userId: _that.userId,
          name: item.name,
          id: item.id,
          type: item.type,
        }
        if (item.type === 'o') {
          obj.orgId = item.orgId;
          delete obj.posId
        } else if (item.type === 'p') {
          obj.posId = item.posId;
          delete obj.orgId
        }
        _that.selApartmentList.push(obj);
      });
    },
    editApartment: function (userId) {
      var _that = this;
      _that.dialogSelApartment = true;
      _that.$refs.searchUserOrgD.setCheckedKeys([], false);
      _that.$refs.searchUserPosD.setCheckedKeys([], false);
      _that.selApartmentList = [];
      _that.apartCheckedExpanded.push(_that.orgPosRootNodeId);
      if (userId) {
        setTimeout(function () {
          _that.selApartmentTreesetCheck(userId);
        }, 800)
      } else {
        _that.$refs.selApartmentTree.setCheckedKeys([], false);
      }
    },
    filterRegionNode: function(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
    },
    editRegion: function(data){
        var _that = this;
        _that.dialogSetRegion = true;
        _that.selRegionList = [];
        _that.filterText3 = '';
        $(".el-tree").animate({scrollTop: 0}, 800);//滚动到顶部
        _that.setRegionExpanded = [];
        _that.setRegionExpanded.push(_that.regionRootNodeId);
        if(data){
            var param = {
                id: data.regionId,
                label: data.regionName,
            }
            _that.selRegionList.push(param);
            _that.$nextTick(function(){
                _that.$refs['setRegionTree'].setCurrentKey(data.regionId);
            });
        }
        if(_that.setRegionTreeData&&_that.setRegionTreeData.length>0){

        }else{
            _that.loadRegion();
        }
    },
    clearSelRegionData: function(){
        var _that = this;
        _that.selRegionList = [];
        _that.filterText3 = '';
        _that.$nextTick(function(){
            _that.$refs['setRegionTree'].setCurrentKey();
        });
    },
    expandAllRegionTree: function () {
        var _that = this;
        _that.setRegionExpanded = [];
        for (var i = 0; i < _that.$refs.setRegionTree.store._getAllNodes().length; i++) {
            _that.$refs.setRegionTree.store._getAllNodes()[i].expanded = true;
            if (_that.$refs.setRegionTree.store._getAllNodes()[i].data.id) {
                _that.setRegionExpanded.push(this.$refs.setRegionTree.store._getAllNodes()[i].data.id)
            }
        }
    },
    closedAllRegionTree: function () {
        var _that = this;
        _that.setRegionExpanded = [];
        for (var i = 0; i < _that.$refs.setRegionTree.store._getAllNodes().length; i++) {
            _that.$refs.setRegionTree.store._getAllNodes()[i].expanded = false;
        }
    },
    loadRegion: function () { // 加载选择部门树数据
        var _that = this;
        _that.setRegionTreeData = [];
        _that.regionRootNodeId = null;
        request('opus-admin', {
            url: ctx + 'bsc/dic/region/gtreeRegion.do',
            type: 'post',
            data: {}
        }, function (data) {
            if(data&&data.length>0){
                _that.setRegionTreeData = data;
                _that.regionRootNodeId = data[0].id;
            }
        }, function () {
            _that.setRegionTreeData = [];
            _that.regionRootNodeId = null;
        });
    },
    initAllRootOrgId: function () { // 初始化顶级组织id
      var _that = this;
      request('opus-admin', {
        url: ctx + 'opu/om/users/initAllRootOrgId',
        type: 'post',
        data: {}
      }, function (data) {
        if (data.success) {

        }
      }, function () {

      });
    },
    getIsFoShan: function () { // 加载选择部门树数据
      var _that = this;
      request('opus-admin', {
        url: ctx + 'opu/om/org/getIsFoShan',
        type: 'get'
      }, function (data) {
        if (data.success) {
          _that.isFoShan = data.content;
        }
      }, function () {

      });
    },
    selRegion: function (data, node, event) {
        var _that = this;
        _that.selRegionList = [];
        _that.selRegionList.push(data);
    },
    removeSelRegion: function () {
        var _that = this;
        _that.selRegionList = [];
    },
    saveSelRegion: function () {
        var _that = this;
        if (_that.selRegionList.length > 0) {
            //_that.orgAllInfo.regionId = _that.selRegionList[0].id;
            //_that.orgAllInfo.regionName = _that.selRegionList[0].label;
            this.$set(_that.orgAllInfo, 'regionId', _that.selRegionList[0].id);
            this.$set(_that.orgAllInfo, 'regionName', _that.selRegionList[0].label);
            _that.dialogSetRegion = false;
        }else{
            _that.$message({
                message: '请选择行政区域！',
                type: 'error'
            });
        }
    },
    loadNode2: function (node, resolve) { // 加载选择部门树数据
      var _that = this;
      if (node.level == 0) {
        request('opus-admin', {url: ctx+'opu/om/org/orgPosAsyncZTree', type: 'get'}, function (data) {
          _that.selApartmentData = [];
          _that.selApartmentData.push(data.content[0]);
          if (data.content[0].id) {
            // _that.apartCheckedExpanded.push(data.content[0].id);
            // _that.userOrgInfoRootId = data.content[0].id;
            _that.orgPosRootNodeId = data.content[0].id;
          }
          resolve(_that.selApartmentData);
          if (!(_that.selApartmentData.length == 0)) {
            _that.$refs.selApartmentTree.setCheckedKeys(_that.apartCheckedList, false);
          }
        }, function () {
          resolve([]);
        });
      } else {
        request('opus-admin', {
          url: ctx+'opu/om/org/orgPosAsyncZTree',
          type: 'get',
          data: {id: node.data.id}
        }, function (data) {
          var selData = [];
          data.content.forEach(function (e) {
            selData.push(e)
          })
          _that.selApartmentData = selData;
          resolve(_that.selApartmentData);
          if (!(_that.selApartmentData.length == 0)) {
            _that.$refs.selApartmentTree.setCheckedKeys(_that.apartCheckedList, false);
          }
        }, function () {
          resolve([]);
        })
      }
    },
    changeApartmentChecked: function (data, checked) { // 修改角色部门
      var _that = this;
      var searchUserOrgD = _that.$refs.searchUserOrgD.getCheckedNodes();
      var searchUserPosD = _that.$refs.searchUserPosD.getCheckedNodes();
      _that.selApartmentList = [];
      _that.apartCheckedList = [];
      var selApartment = _that.$refs.selApartmentTree.getCheckedNodes();
      selApartment.map(function (item) {
        if (_that.apartCheckedList.indexOf(item.id) == -1) {
          _that.apartCheckedList.push(item.id);
          var obj = {
            userId: _that.userId,
            name: item.name,
            id: item.id,
            type: item.type,
          }
          if (item.type === 'o') {
            obj.orgId = item.orgId;
            delete obj.posId
          } else if (item.type === 'p') {
            obj.posId = item.posId;
            delete obj.orgId
          }
          _that.selApartmentList.push(obj);
        }
      });
      searchUserOrgD.map(function (item) {
        if (_that.apartCheckedList.indexOf(item.id) == -1) {
          _that.apartCheckedList.push(item.id);
          var obj = {
            userId: _that.userId,
            name: item.name,
            id: item.id,
            type: item.type,
          }
          if (item.type === 'o') {
            obj.orgId = item.orgId;
            delete obj.posId
          } else if (item.type === 'p') {
            obj.posId = item.posId;
            delete obj.orgId
          }
          _that.selApartmentList.push(obj);
        }
      });
      searchUserPosD.map(function (item) {
        if (_that.apartCheckedList.indexOf(item.id) == -1) {
          _that.apartCheckedList.push(item.id);
          var obj = {
            userId: _that.userId,
            name: item.name,
            id: item.id,
            type: item.type,
          }
          if (item.type === 'o') {
            obj.orgId = item.orgId;
            delete obj.posId
          } else if (item.type === 'p') {
            obj.posId = item.posId;
            delete obj.orgId
          }
          _that.selApartmentList.push(obj);
        }
      });
    },
    nodeClick:function(data,checked,node){
      // this.checkedId = data.id
      this.$refs.selRegionTree.setCheckedNodes([data]);
    },
    handleClick:function(data, checked, node){
      if(checked == true){
        // this.checkedId = data.id;
        this.$refs.selRegionTree.setCheckedNodes([data]);
      }
    },
    changeRegionChecked: function (data, checked) { // 修改行政区域
      var _that = this;
      _that.selRegionList = [];
      var selRegion = _that.$refs.selRegionTree.getCheckedNodes();
      selRegion.map(function (item) {
        _that.regionCheckedList.push(item.id);
        _that.selRegionList.push({
          regionId:item.id,
          name:item.name,
          orgId: _that.orgId
        })
      });
    },
    // removeSelRegion: function (item, ind) {
    //   var _that = this;
    //   this.selRegionList.splice(ind, 1);
    //   _that.selRegionList = [];
    //   this.selRegionList.map(function (e) {
    //     _that.selRegionList.push(e.id);
    //   });
    //   _that.$refs.selRegionTree.setCheckedKeys(_that.selRegionList, false);
    // },
    clearSelData: function () {
      if (this.userId) this.getApartmentList(this.userId);
      this.filterText1 = '';
    },
    removeSelOrg: function (item, ind) {
      var _that = this;
      this.selApartmentList.splice(ind, 1);
      _that.apartCheckedList = [];
      this.selApartmentList.map(function (e) {
        _that.apartCheckedList.push(e.id);
      });
      _that.$refs.selApartmentTree.setCheckedKeys(_that.apartCheckedList, false);
      _that.$refs.searchUserOrgD.setCheckedKeys(_that.apartCheckedList, false);
      _that.$refs.searchUserPosD.setCheckedKeys(_that.apartCheckedList, false);
    },
    saveUserOrgSelInfo: function () {
      var _that = this;
      _that.isDisabled = true;
      var param = JSON.stringify(this.selApartmentList);
      if(!(this.selApartmentList.length>0)){
        _that.$message({
          message: '请至少选择一个部门！',
          type: 'error'
        });
        _that.isDisabled = false;
        return false;
      }
      request('opus-admin', {
        url: ctx+'civicism/linkman/saveUserOrgOrPosRelation',
        ContentType: 'application/json',
        type: 'post',
        data: param
      }, function (data) {
        if (data.success) {
          _that.$message({
            message: '保存成功',
            type: 'success'
          });
        } else {
          _that.$message({
            message: data.message ? data.message:'保存失败',
            type: 'error'
          });
        }
        _that.dialogSelApartment = false;
        _that.isDisabled = false;
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '服务请求失败',
          type: 'error'
        });
        _that.isDisabled = false;
      });
    },
    saveRegionSelInfo: function () {
      var _that = this;
      _that.isDisabled = true;
      var param = JSON.stringify(this.selRegionList);
      if(!(this.selRegionList.length>0)){
        _that.$message({
          message: '请选择一个区域！',
          type: 'error'
        });
        _that.isDisabled = false;
        return false;
      }
      request('opus-admin', {
        url: ctx+'opu/om/org/updateOpuOmOrgByRegionId',
        // ContentType: 'application/json',
        type: 'post',
        data:{
          orgId:this.selRegionList[0].orgId,
          regionId:this.selRegionList[0].regionId
        }
      }, function (data) {
        if (data.success) {
          _that.$message({
            message: '保存成功',
            type: 'success'
          });
          _that.regionList = [];
          _that.regionList.push({
            name:_that.selRegionList[0].name,
            id:_that.selRegionList[0].orgId
          })
        } else {
          _that.$message({
            message: data.message ? data.message:'保存失败',
            type: 'error'
          });
        }

        _that.dialogSelRegion = false;
        _that.isDisabled = false;
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '服务请求失败',
          type: 'error'
        });
        _that.isDisabled = false;
      });
    },
    saveUserInfo: function () {
      var _that = this;
      _that.personBaseInfo.isActive = _that.personBaseInfo.isActive ? '1' : '0';
      _that.personBaseInfo.isPwdEncrypted = _that.personBaseInfo.isPwdEncrypted ? '1' : '0';
      // _that.personBaseInfo.loginPwd = _that.loginPassword ? _that.loginPassword : _that.personBaseInfo.loginPwd;
      _that.$refs['userInfoForm'].validate(function (valid) {
        if (valid) {
          _that.$refs['personBaseInfoForm'].validate(function (validBase) {
            if(validBase){
              _that.submitUserInfo(_that.personBaseInfo);
            }else {
              alertMsg('', '请输入完整且正确的信息', '关闭', 'warning', true);
              return false;
            }
          });
        } else {
          alertMsg('', '请输入完整且正确的信息', '关闭', 'warning', true);
          return false;
        }
      });
    },
    submitUserInfo: function (data) {
      var _that = this;
      _that.isDisabled = true;
      request('opus-admin', {
        url: ctx+'civicism/linkman/saveLinkmanAndOmUser?pId=' + _that.id + '&&type=' + _that.orgType,
        ContentType: 'application/json',
        type: 'post',
        data: JSON.stringify(data)
      }, function (result) {
        if (result.success) {
            var userInfo = result.content;
          _that.$message({
            message: data.userId?"保存成功！":"保存成功,初始化密码：" + userInfo.loginPwd,
            type: 'success'
          });

          _that.showOverlay = false;
          _that.showRightInfo = false;
        } else {
          _that.$message({
            message: result.message ? result.message : '保存失败',
            type: 'error'
          });
        }
        _that.dialogSelApartment = false;
        _that.isDisabled = false;
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message : '服务请求失败',
          type: 'error'
        });
        _that.isDisabled = false;
      });
    },
    // userPassword: function (val) {
    //   this.personBaseInfo.loginPwd = val;
    //   this.loginPassword = val;
    // },
    saveOpuAcRoleUser: function () {
      var _that = this;
      var sendData = {
        userId: _that.userId,
        appSoftId: _that.selOpuAcRole,
        roleIds: _that.selRoleItemIDs
      };
      _that.isDisabled = true;
      request('opus-admin', {
        url: ctx+'opu/om/users/assignRoleToUser',
        type: 'post',
        data: sendData
      }, function (data) {
        if (data) {
          _that.$message({
            message: '修改成功！',
            type: 'success'
          });
        } else {
          _that.$message({
            message: data.message ? data.message:'修改失败！',
            type: 'error'
          });
        }
        _that.isDisabled = false;
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message:'请求失败！',
          type: 'error'
        });
        _that.isDisabled = false;
      });
    },
    opuAcRoleUserHandleClick: function () {
      if (this.infoSideActive == 'opuAcRoleUser') {
        var _that = this;
        request('opus-admin', {url: ctx+'opus/rs/app/listAppInstByCurrentOrgId.do', type: 'post'}, function (data) {
          if (data) {
            _that.selOpuAcRoleList = data;
            _that.selOpuAcRole = data[0].appSoftId;
            if (_that.firstGetSelOpuAcRole) {
              _that.getOpuAcRoleItem(data[0].appSoftId);
            }
          }
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message:'请求失败！',
            type: 'error'
          });
        });
      }
    },
    getOpuAcRoleItem: function (val) {
      var _that = this;
      var sendData = {
        userId: this.userId,
        appSoftId: val
      };
      request('opus-admin', {
        url: ctx+'opu/om/users/listAllRolesNoPageForUser',
        type: 'get',
        data: sendData
      }, function (data) {
        _that.OpuAcRoleItem = data.content;
        _that.$nextTick(function () {
          _that.OpuAcRoleItem.forEach(function (e) {
            if (e.isCheck) {
              _that.$refs.multipleTable.toggleRowSelection(e, true)
            }
          })
        });
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message:'请求失败！',
          type: 'error'
        });
      });
    },
    handleSelRoleItemChange: function (val) {  // 角色授权选中tr事件
      var ids = new Array();
      val.map(function (item) {
        ids.push(item.roleId)
      })
      this.selRoleItemIDs = ids.join(',');
    },
    saveOrgInfo: function () {  // 保存组织信息
      var _that = this;
      var parentOrgId = this.orgAllInfo.parentOrgId;
      _that.$refs['userOrgForm'].validate(function (valid) {
        if (valid) {
          request('opus-admin', {
            url: ctx+'civicism/unit/saveUnitAndOmOrg',
            ContentType: 'application/json',
            data: JSON.stringify(_that.orgAllInfo)
          }, function (data) {
            if (data.success) {
              _that.$message({
                message: '保存成功',
                type: 'success'
              });
              _that.getListData(parentOrgId, true);
              _that.showOverlay = false;
              _that.showRightOrgInfo = false;
              _that.resetForm('userOrgForm');

              // 新增时候并且保存成功
              if(!_that.editOrgInfoFlag&&data.content.isActive=='1'){

                _that.$refs.treeLeft.append(data.content, parentOrgId);

              // 编辑的时候并且保存成功
              }else if(_that.editOrgInfoFlag&&data.content.isActive=='1'){

                  if(data.content.orgLevel==1){
                      window.location.reload();
                  }else{
                      _that.$refs.treeLeft.remove(data.content);
                      _that.$refs.treeLeft.append(data.content, parentOrgId);
                  }
              }else if(data.content.isActive=='0'){

                  _that.$refs.treeLeft.remove(data.content);
              }
            } else {
              _that.$message({
                message: data.message ? data.message : '保存失败',
                type: 'error'
              });
            }
          }, function (msg) {
            _that.$message({
              message: msg.message ? msg.message:'请求失败！',
              type: 'error'
            });
          });
        } else {
          alertMsg('', '请输入完整且正确的信息', '关闭', 'warning', true);
          return false;
        }
      });
    },
    loadNode3: function (node, resolve) { // 加载选择部门树数据
      var _that = this;
      if (node.level == 0) {
        request('opus-admin', {url: ctx+'opu/om/org/orgPosUserAsyncZTree', type: 'get'}, function (data) {
          _that.setAdminTreeData = [];
          _that.setAdminTreeData.push(data.content[0]);
          if (data.content[0].id) {
            _that.setAdminRootNodeId = data.content[0].id;
          }
          resolve(_that.setAdminTreeData);
        }, function () {
          resolve([]);
        });
      } else {
        request('opus-admin', {
          url: ctx+'opu/om/org/orgPosUserAsyncZTree',
          type: 'get',
          data: {id: node.data.id, type: node.data.type}
        }, function (data) {
          var selData = [];
          data.content.forEach(function (e) {
            selData.push(e)
          })
          _that.setAdminTreeData = selData;
          resolve(_that.setAdminTreeData);
        }, function () {
          resolve([]);
        })
      }
    },
    loadNode4: function (node, resolve) {
      var _that = this;
      if (node.level == 0) {
        request('opus-admin', {url: ctx+'opu/om/org/orgAsyncZtree', type: 'get'}, function (data) {
          _that.treeData = [];
          data.content.forEach(function (e) {
            e.disabled = true; // 禁止选中
            _that.treeData.push(e)
          })
          _that.treeShowNode[0] = _that.treeData[0].id;
          if (_that.isFirst) {
            _that.orgName = _that.treeData[0].orgName;
            _that.id = _that.treeData[0].id;
            _that.orgType = _that.treeData[0].type;
            _that.rootNodeOrgCode = _that.treeData[0].orgCode;
            _that.rootNodeOrgId = _that.treeData[0].id;
            _that.userCount = _that.treeData[0].userCount;
            _that.navItemObj = [
              {
                pId: _that.treeData[0].pId,
                name: _that.treeData[0].orgName,
                id: _that.treeData[0].orgId,
                userCount: _that.treeData[0].userCount
              }
            ];
            if(_that.navItemObj.length>0){
              _that.orgIdP = _that.navItemObj[_that.navItemObj.length-1].id;
            }            _that.getListData(_that.treeData[0].id)
            _that.isFirst = false
          }
          resolve(_that.treeData);
          _that.loading = false;
        }, function () {
          resolve([]);
          _that.loading = false;
        });
      } else {
        request('opus-admin', {
          url: ctx+'opu/om/org/orgAsyncZtree',
          type: 'get',
          data: {id: node.data.id}
        }, function (data) {
          var myList = [];
          if (data.content[0]) {
            _that.parentId = data.content[0].pId;
          }
          data.content.forEach(function (e) {
            myList.push(e)
          })
          _that.treeData = myList;
          resolve(_that.treeData);
          _that.loading = false;
        }, function () {
          resolve([]);
          _that.loading = false;
        })
      }
    },
    setOrgAdmin: function () {  // 设置组织管理员
      this.dialogSetAdmin = true;
      var _that = this;
      _that.setAdminExpanded = [];
      _that.selSetAdminCheckedList = [];
      _that.setAdminList = [];
      _that.closedAllAdminTree();
      _that.setAdminExpanded.push(_that.setAdminRootNodeId);
      var setAdminExpandedStr = '';
      request('opus-admin', {
        url: ctx+'opu/om/org/listOpuOmOrgAdminByOrgId',
        type: 'get',
        data: {orgId: _that.id}
      }, function (data) {
        if (data.success) {
          _that.setAdminList = data.content;
          _that.baseSeachAdminList = data.content;
          data.content.map(function (e) {
            setAdminExpandedStr = setAdminExpandedStr + e.userOrgIds + ',';
            e.userOrgIds.split(",").map(function (item) {
              _that.selSetAdminCheckedList.push(e.userId + item);
            })
          });
          _that.setAdminExpanded = setAdminExpandedStr.split(",");
        }
        _that.$refs.setAdminTree.setCheckedKeys(_that.selSetAdminCheckedList, false);
      }, function (msg) {
        _that.$message({
          message: msg.message ? msg.message:'请求失败！',
          type: 'error'
        });
      });
    },
    addDept:function (){
      var _that = this;
      this.dialogAddDept = true;
      this.filterText = '';
      this.$nextTick(function(){
        _that.$refs.addDeptTree.setCheckedKeys(_that.setAdminList, false);
        _that.setAdminList = [];
      })
    },
    expandAllDeptTree: function () {
      var _that = this;
      this.expandAllAdminNode = true;
      _that.setAdminExpanded = [];
      for (var i = 0; i < this.$refs.addDeptTree.store._getAllNodes().length; i++) {
        this.$refs.addDeptTree.store._getAllNodes()[i].expanded = this.expandAllAdminNode;
        if (this.$refs.addDeptTree.store._getAllNodes()[i].data.id) {
          _that.setAdminExpanded.push(this.$refs.addDeptTree.store._getAllNodes()[i].data.id)
        }
      }
    },
    closedAllDeptTree: function () {
      var _that = this;
      this.expandAllAdminNode = false;
      for (var i = 0; i < this.$refs.addDeptTree.store._getAllNodes().length; i++) {
        this.$refs.addDeptTree.store._getAllNodes()[i].expanded = this.expandAllAdminNode;
        _that.setAdminExpanded = [];
      }
    },
    removeSelAdmin: function (item, ind) { // 移除已选管理员
      var _that = this;
      var setAdminKeys = this.$refs.setAdminTree.getCheckedKeys();
      setAdminKeys.map(function (e, index) {
        if (e.indexOf(item.userId) !== -1) {
          setAdminKeys.splice(index, 1)
        }
      })
      this.setAdminList.splice(ind, 1)
      _that.$refs.setAdminTree.setCheckedKeys(setAdminKeys, false);
    },

    removeSelDept: function (item, ind) { // 移除已选部门
      var _that = this;
      var setAdminKeys = this.$refs.addDeptTree.getCheckedKeys();
      setAdminKeys.map(function (e, index) {
        if (e.indexOf(item.orgId) !== -1) {
          setAdminKeys.splice(index, 1)
        }
      })
      this.setAdminList.splice(ind, 1);
      _that.$refs.addDeptTree.setCheckedKeys(setAdminKeys, false);
    },

    changeAdminChecked: function (data, checked) { // 修改组织管理员
      var _that = this;
      var setAdminsel = _that.$refs.setAdminTree.getCheckedNodes();
      var searchAdminsel = _that.$refs.searchAdminId.getCheckedNodes();
      var newArr = [];
      if (_that.setAdminSearchFlag) {
        _that.setAdminList = [];
        setAdminsel.map(function (item) {
          _that.setAdminList.push({userId: item.userId, userName: item.userName, orgId: _that.id})
        });
      } else {
        var searchAdminArr = [];
        var flag1 = true;
        _that.setAdminList = _that.baseSeachAdminList;
        searchAdminsel.map(function (item) {
          searchAdminArr.push({userId: item.userId, userName: item.userName, orgId: _that.id})
        });
        _that.setAdminList = _that.setAdminList.concat(searchAdminArr);
      }
      for (var i = 0; i < _that.setAdminList.length; i++) {
        var flag = true;
        for (var j = 0; j < newArr.length; j++) {
          if (_that.setAdminList[i].userId == newArr[j].userId) {
            flag = false;
          }
          ;
        }
        ;
        if (flag) {
          newArr.push(_that.setAdminList[i]);
        }
        ;
      }
      ;
      _that.setAdminList = newArr;
    },
    changeDeptChecked: function (data, checked) {
      var _that = this;
      var setAdminsel = _that.$refs.addDeptTree.getCheckedNodes();
      var searchAdminsel = _that.$refs.addDeptTree.getCheckedNodes();

      if (_that.leftOrgTree) {
        _that.setAdminList = [];
        setAdminsel.map(function (item) {
          _that.setAdminList.push({name: item.name, orgName: item.orgName, orgId: item.orgId})
        });
      } else {
        var searchAdminArr = [];
        var flag1 = true;
        _that.setAdminList = _that.baseSeachAdminList;
        searchAdminsel.map(function (item) {
          searchAdminArr.push({name: item.name, orgName: item.orgName, orgId: _that.orgId})
        });
        _that.setAdminList = _that.setAdminList.concat(searchAdminArr);
      }
    },
    changeDeptSel: function (data, checked,ischeckChild) {
      var setAdminKeys = this.$refs.addDeptTree.getCheckedKeys();
      if (!checked && data.orgId) {
        setAdminKeys.map(function (item, ind) {
          if (item.indexOf(data.orgId) !== -1) {
            setAdminKeys.splice(ind, 1)
          }
        })
        this.$refs.addDeptTree.setCheckedKeys(setAdminKeys, false);
      } else {
        if(data.orgLevel != 2 && data.orgLevel != 1){
          var idKeys = [data.parentOrgId] || [];
        }
        var setidKeys = this.$refs.addDeptTree.getCheckedKeys();
        if (data.orgId) {
          var setIds = setidKeys.concat(idKeys)
          this.$refs.addDeptTree.setCheckedKeys(setIds, false);
        }
      }
    },
    saveSetAdmin: function () {
      var _that = this;
      _that.isDisabled = true;
      var param = JSON.stringify(this.setAdminList);
      if(!(this.setAdminList.length>0)){
        request('opus-admin', {
          url: ctx+'opu/om/org/cancelOpuOmOrgAdmin',
          type: 'post',
          data: {orgId: _that.id}
        }, function (data) {
          if (data.success) {
            _that.$message({
              message: '保存成功',
              type: 'success'
            });
          } else {
            _that.$message({
              message: data.message ? data.message:'保存失败',
              type: 'error'
            });
          }
          _that.dialogSetAdmin = false;
          _that.isDisabled = false;
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message:'请求失败',
            type: 'error'
          });
          _that.isDisabled = false;
        });
      }else {
        request('opus-admin', {
          url: ctx+'opu/om/org/batchOpuOmOrgAdmin',
          ContentType: 'application/json',
          type: 'post',
          data: param
        }, function (data) {
          if (data.success) {
            _that.$message({
              message: '保存成功',
              type: 'success'
            });
          } else {
            _that.$message({
              message: data.message ? data.message:'保存失败',
              type: 'error'
            });
          }
          _that.dialogSetAdmin = false;
          _that.isDisabled = false;
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message:'请求失败',
            type: 'error'
          });
          _that.isDisabled = false;
        });
      }
    },
    saveDept:function (){
      var _that = this;
      var orgIdsArry = _that.$refs.addDeptTree.getCheckedKeys();
      _that.saveUnitRelation(orgIdsArry);
    },
    filterNodeOrg:function(value, data) {
      if (!value) {
        return true;
      }
      return data.name.indexOf(value) !== -1;
    },
    clearSearchAdmin: function () {
      this.filterText2 = '';
      this.setAdminSearchFlag = true;
    },
    changeEnableType:function(data){
      data.isActive = data.isActive == 0 ? 1 : 0
    },
    changeRejectType:function(data){
      data.isRejectSms = (data.isRejectSms==null||data.isRejectSms == 0) ? 1 : 0
    },
    changeRemindType:function(data){
      data.enableStrongRemind = (data.enableStrongRemind==null||data.enableStrongRemind == 0) ? 1 : 0
    },
    resetForm: function (formName) {
      var _that = this;
      this.$nextTick(function(){
        _that.$refs[formName].resetFields();
      })
    }
  },
  watch: {
    filterText: function (value) {
      if (!value) {
        this.leftOrgTree = true;
        this.searchManTree = [];
        this.searchOrgTree = [];
        return;
      };
      var _that = this;
      request('opus-admin', {
        url: ctx+'opu/om/org/listOrgsAndUsersByKeyword',
        type: 'get',
        data: {keyword: value}
      }, function (res) {
        _that.leftOrgTree = false;
        var searchOrgTree = [];
        var searchManTree = [];
        res.content.forEach(function (e) {
          if (e.type == 'u') {
            searchManTree.push(e);
          } else {
            searchOrgTree.push(e);
          }
        });
        _that.searchManTree = searchManTree;
        _that.searchOrgTree = searchOrgTree;
      }, function (msg) {
        _that.leftOrgTree = true;
        _that.$message({
          message: '请求失败！',
          type: 'error'
        });
      });
    },
    filterText1: function (value) {
      var _that = this;
      if (value) {
        request('opus-admin', {
          url: ctx+'opus/om/org/getOpuOmActiveOrgsAndPosesByKeyWord.do',
          type: 'get',
          data: {name: value}
        }, function (res) {
          _that.userOrgInfoFlag = false;
          var userSearchOrgTree = [];
          var userSearchPosTree = [];
          res.forEach(function (e) {
            if (e.type == 'p') {
              userSearchPosTree.push(e)
            } else {
              userSearchOrgTree.push(e)
            }
          });
          _that.userSearchPosTree = userSearchPosTree;
          _that.userSearchOrgTree = userSearchOrgTree;
        }, function (msg) {
          this.userOrgInfoFlag = true;
          _that.$message({
            message: msg.message ? msg.message:'请求失败！',
            type: 'error'
          });
        });
      } else {
        this.userOrgInfoFlag = true;
      }
    },
    filterText2: function (value) {
      var _that = this;
      var setAdminKeys = this.$refs.setAdminTree.getCheckedKeys();
      if (value) {
        request('opus-admin', {
          url: ctx+'opu/om/users/listOpuOmActiveUsersByKeyWord',
          type: 'get',
          data: {keyword: value}
        }, function (res) {
          _that.searchAdminTree = res.content;
          _that.setAdminSearchFlag = false;
          var searchAdminIdCkecked = [];
          res.content.map(function (e) {
            setAdminKeys.map(function (item) {
              if (item.indexOf(e.userId) !== -1) {
                searchAdminIdCkecked.push(e.userId);
              }
            })
          });
          _that.$refs.searchAdminId.setCheckedKeys(searchAdminIdCkecked, false)
        }, function (msg) {
          _that.$message({
            message: msg.message ? msg.message:'请求失败！',
            type: 'error'
          });
          _that.setAdminSearchFlag = true;
        });
      } else {
        _that.setAdminSearchFlag = true;
      }
    },
    filterText3: function (value) {
      this.$refs.setRegionTree.filter(value);
    },
  }
});